package com.zhxy.util.bean.valid;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;


public class ValidationUtil {
	
	private static Validator validator = null;
	
	private static Object lock = new Object();
	
	public static Validator getValidator() {
		if (validator == null) {
			synchronized (lock) {
				if (validator == null) {
					validator = Validation.buildDefaultValidatorFactory().getValidator();
				}
			}
		}
		return validator;
	}
	
	public static <T> String validate(T object){
		Set<ConstraintViolation<T>> r = getValidator().validate(object);
		if (r != null && r.size() > 0) {
			return r.iterator().next().getMessage();
		}
		return null;
	}
}
